﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

public partial class PresentationLayer_DefaultTab : System.Web.UI.Page
{
    DataLoggerBLL bll = new DataLoggerBLL();
    DMHuongDongChayDAL DMHuongDongChayDal = new DMHuongDongChayDAL();
    DataLoggerEntities DataLoggerEntities = new DataLoggerEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DataTable"] == "")
                Response.Redirect("../default.aspx");
            for (int i = DateTime.Now.Year; i >= 1990; --i)
            {
                ListItem iteml = new ListItem(i.ToString(), i.ToString());
                DListNamSanXuat.Items.Add(iteml);
                DListNamLapDat.Items.Add(iteml);
            }
            DataLoggerEntities = bll.GetDataLoggerEntitiesByID();
            txtObjectID.Value = DataLoggerEntities.OBJECTID.ToString();
            txtIDDataLogger.Text = DataLoggerEntities.IDDataLogger;

            foreach (var itemc in DMHieuDataLoggerDAL.GetList())
            {
                if (DataLoggerEntities.NhanHieuText.Trim() == itemc.MoTa.ToString().Trim())
                    DataLoggerEntities.NhanHieu = itemc.Ma;
                ListItem iteml = new ListItem(itemc.MoTa.ToString(), itemc.Ma.ToString());
                DListNhanHieu.Items.Add(iteml);
            }

            DListNhanHieu.SelectedValue = DataLoggerEntities.NhanHieu.ToString();

            this.txtTieuChuan.Text = DataLoggerEntities.TieuChuan;

            foreach (var itemnsx in DMNhaSanXuatDAL.GetList())
            {
                if (DataLoggerEntities.NhaSanXuatText.ToString().Trim() == itemnsx.MoTa.ToString().Trim())
                    DataLoggerEntities.NhaSanXuat = itemnsx.Ma;
                ListItem iteml = new ListItem(itemnsx.MoTa.ToString(), itemnsx.Ma.ToString());
                DListNhaSX.Items.Add(iteml);
            }
            DListNhaSX.SelectedValue = DataLoggerEntities.NhaSanXuat.ToString();

            foreach (var itemnusx in DMNuocSanXuatDAL.GetList())
            {
                if (DataLoggerEntities.NuocSanXuatText.ToString().Trim() == itemnusx.MoTa.ToString().Trim())
                    DataLoggerEntities.NuocSanXuat = itemnusx.Ma;
                ListItem iteml = new ListItem(itemnusx.MoTa.ToString(), itemnusx.Ma.ToString());
                DListNuocSX.Items.Add(iteml);
            }
            DListNuocSX.SelectedValue = DataLoggerEntities.NuocSanXuat.ToString();

            DListNamSanXuat.SelectedValue = DataLoggerEntities.NamSanXuat.ToString();

            foreach (var itemnttsd in DMTinhTrangSDDAL.GetList())
            {
                if (DataLoggerEntities.TinhTrangText.ToString().Trim() == itemnttsd.MoTa.ToString().Trim())
                    DataLoggerEntities.TinhTrang = itemnttsd.Ma;
                ListItem iteml = new ListItem(itemnttsd.MoTa.ToString(), itemnttsd.Ma.ToString());
                DListTTSD.Items.Add(iteml);
            }
            DListTTSD.SelectedValue = DataLoggerEntities.TinhTrang.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (DataLoggerEntities.DonViQLText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                    DataLoggerEntities.DonViQL = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViQL.Items.Add(iteml);
            }
            DListDonViQL.SelectedValue = DataLoggerEntities.DonViQL.ToString();

            foreach (var itemtsh in DMThuocSoHuuDAL.GetList())
            {
                if (DataLoggerEntities.ThuocSoHuuText.ToString().Trim() == itemtsh.MoTa.ToString().Trim())
                    DataLoggerEntities.ThuocSoHuu = itemtsh.Ma;
                ListItem iteml = new ListItem(itemtsh.MoTa.ToString(), itemtsh.Ma.ToString());
                DListThuocSoHuu.Items.Add(iteml);
            }
            DListThuocSoHuu.SelectedValue = DataLoggerEntities.ThuocSoHuu.ToString();
            this.txtMoTaViTri.Text = DataLoggerEntities.MoTaViTri.ToString();

            DListNamLapDat.SelectedValue = DataLoggerEntities.NamLapDat.ToString();

            this.txtIDDongHoTong.Text = DataLoggerEntities.IDDongHoTong.ToString();

            this.txtNgayCNC.Text = DataLoggerEntities.NgayCNCuoi.ToString("dd/MM/yyyy");
            this.txtNguoiCNC.Text = DataLoggerEntities.NguoiCNCuoi.ToString();
            this.nSoLanSuCo.Text = DataLoggerEntities.SoLanSuCo.ToString();
            this.txtIDHinhAnh.Text = DataLoggerEntities.IDHinhAnh.ToString();


        }
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        DataLoggerEntities bk = new DataLoggerEntities();
        DataLoggerDAL DataLoggerDAL = new DataLoggerDAL();
        //DataLoggerEntities DataLoggerEntities = new DataLoggerEntities(Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue!=""?DListDonViTC.SelectedValue:"0"),
        //    Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue!=""?DListDonViGS.SelectedValue:"0"), this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
        DataLoggerEntities DataLoggerEntities = new DataLoggerEntities(Convert.ToInt32(txtObjectID.Value),this.txtIDDataLogger.Text,
                                  Convert.ToInt32(DListNhanHieu.SelectedValue = DListNhanHieu.SelectedValue != "" ? DListNhanHieu.SelectedValue : "0"),
                                  this.txtTieuChuan.Text, Convert.ToInt32(DListNhaSX.SelectedValue = DListNhaSX.SelectedValue != "" ? DListNhaSX.SelectedValue : "0"),
                                  Convert.ToInt32(DListNuocSX.SelectedValue = DListNuocSX.SelectedValue != "" ? DListNuocSX.SelectedValue : "0"),
                                  Convert.ToInt32(DListNamSanXuat.SelectedValue),Convert.ToInt32(DListTTSD.SelectedValue = DListTTSD.SelectedValue != "" ? DListTTSD.SelectedValue : "0"),
                                  Convert.ToInt32(DListDonViQL.SelectedValue = DListDonViQL.SelectedValue != "" ? DListDonViQL.SelectedValue : "0"),
                                  Convert.ToInt32(DListThuocSoHuu.SelectedValue = DListThuocSoHuu.SelectedValue != "" ? DListThuocSoHuu.SelectedValue : "0"),
                                  this.txtMoTaViTri.Text, Convert.ToInt32(DListNamLapDat.SelectedValue), this.txtIDDongHoTong.Text, DateTime.Parse(Request.Form.Get("txtNgayCNC")),
                                  this.txtNguoiCNC.Text,Convert.ToInt32(this.nSoLanSuCo.Text),this.txtIDHinhAnh.Text);
        DataLoggerDAL.Update1(DataLoggerEntities);
        //DataLoggerDAL.Update(Convert.ToInt32(txtObjectID.Value), txtMaDoanOng.Text, txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue), Convert.ToDouble(this.nChieuDai.Text));
      //  Response.Redirect("../default.aspx");
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Cập nhật thành công'); window.location.href ='../Default.aspx';</script>");
    }
    protected void btnHuyBo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../default.aspx");
    }
}